/**
 * zhangqiang    2017.04.26
 * 空白页面
 */
import React, { Component } from 'react'
import { connect } from 'react-redux'
import Login from '../../utils/loginOperation_bak'
import { orderDetails_view } from '../../actions/orderDetails';

// 加载组件
import { Toast } from 'antd-mobile'

//依赖
import jsApi from '../../utils/cx580.jsApi'
import Common from '../../utils/common'

/**
 * 组件
 */

class JumpUrl extends Component {
	constructor(props) {
		super(props);
		this.getUrlParam = this.getUrlParam.bind(this)
		this.state = {
		};
		this.orderId = this.getUrlParam('OutTradeNo');
	}


	// 取得某个url参数
	getUrlParam(name, url) {
		var urlSearch = '?' + window.location.href.split('?')[1];
		var search = url || urlSearch.substr(1);

		if (search != '') {
			var re = new RegExp('(^|&|\\?)' + name + '=([^&]*)($|&)');
			var arr = search.match(re);
			if (arr !== null) {
				return decodeURI(arr[2]);
			}
		}
		return '';
	}
	sendMessage(type) {
		window.cxytj.recordUserBehavior({
			eventId: type,
			eventType: '2', //事件类型
			eventTime: '',  //触发时间
		});
	}
	/**
	 * 生命周期
	 */
	componentDidMount() {
		Toast.loading('请稍等');
		//localStorage.setItem('detailUserType', 'app')
		Login.appLogin(() => {
			//详情
			this.props.dispatch(orderDetails_view({
				orderId: this.getUrlParam('OutTradeNo'),
			}, (data) => {
				if (data.confirmStatus == 0) {
					try {
						let urlOfNow = window.location.href;
						let APPbaseApiUrl = urlOfNow.replace(window.location.hash, '').split('?')[0].replace('#/', '');
                        let urls = this.getUrlParam('OutTradeNo');
                        jsApi.call({
                            "commandId": "",
                            "command": "close",
                            "data": {}
                        }, function () { });


						//alert(APPbaseApiUrl);
						jsApi.call({
							'commandId': '',
							'command': 'openNewBrowserWithURL',
							'data': {
								'url': `${APPbaseApiUrl}#/annualInspection/${this.orderId}/shut`,
								'umengId': 'cfw_youkachongzhi',
								'showTitleLayout': 'false' //是否显示到导航栏
							}
						}, function (data) {

						});
					} catch (error) {
						this.context.router.push(`annualInspection/${this.orderId}/shut`);
					}

					//	alert(window.location.href);

					//this.context.router.push(`annualInspection/${this.orderId}/shut`);

					this.sendMessage('insepection_orderdetail_continue');

				} else if (data.confirmStatus == 1) {
					if (data.inspectionType == 2) {
						this.context.router.push(`sendTheAddress/overLine/${this.orderId}`);
					} else {
						this.context.router.push(`mailingAddress/${this.orderId}/shut`);
					}

					this.sendMessage('insepection_orderdetail_continue');
				} else if (data.confirmStatus == 2) {
					if (data.inspectionType == 2) {
						this.context.router.push(`sendTheAddress/overLine/${this.orderId}`);
					} else {
						if (data.mailType == 'ems') { //邮政
							//this.context.router.push(`orderDetails/?OutTradeNo=${this.orderId}`);
							this.context.router.push(`sendTheAddress/ems/btnNo/${this.orderId}/shut`);
							this.sendMessage('insepection_orderdetail_checkdoc');
						} else if (data.mailType == 'person') { //自主
							//this.context.router.push(`orderDetails/?OutTradeNo=${this.orderId}`);
							this.context.router.push(`sendTheAddress/person/btnNo/${this.orderId}/shut`);
							this.sendMessage('insepection_orderdetail_checkmyaddress');
						}
					}
				}
			}))

			this.sendMessage('inspection_orderdetail_init');
		})
	}
	/**
	 * 事件 
	 */


	/**
	 * 组件
	 */
	render() {

		return (
			<div>
			</div>
		)
	}
}

JumpUrl.contextTypes = {
	router: React.PropTypes.object.isRequired
}

JumpUrl.defaultProps = {

};

export default connect()(JumpUrl)